package com.sunther.document.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.jwt.JWT;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sunther.document.entity.FileEntity;
import com.sunther.document.service.FileService;
import com.sunther.document.util.UserInfoUtil;
import com.sunther.idb.common.util.WebResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author csb
 * @description: TODO 文件相关
 * @date 2023/5/24 10:10
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @GetMapping("/page_list")
    public WebResult pageList(@RequestParam(required = false) String keyword,
                              @RequestParam(required = false) String tagName,
                              @RequestParam(required = false) Integer state,
                              @RequestParam(required = false) String systemId,
                              @RequestParam(required = false) Integer uploadUserId,
                              @RequestParam(required = false) String selectProId,
                              @RequestParam int pageNumber,
                              @RequestParam int pageSize,
                              HttpServletRequest request) {
        try {
            return WebResult.success("操作成功",fileService.pageList(request,keyword,tagName,state,systemId,uploadUserId,selectProId,pageNumber,pageSize));
        }catch (Exception e) {
            return WebResult.failure("操作失败");
        }

    }

    @PostMapping("/save")
    public WebResult save(@RequestBody FileEntity data, HttpServletRequest request) {
        return fileService.saveFileData(data,request);
    }

    @PostMapping("/upload")
    public WebResult upload(MultipartFile[] files) throws Exception {
        return fileService.upload(files);
    }

    @GetMapping("downloadFile")
    public void downloadFile(HttpServletRequest request,
                             HttpServletResponse response,
                             @RequestParam String id,
                             @RequestParam String fileId,
                             @RequestParam Integer roleId) throws IOException {
        fileService.downloadFile(id,fileId,roleId,request,response);
    }

    @GetMapping("/cancel")
    public WebResult cancel(@RequestParam(value = "id") String id) {
        return fileService.cancel(id);
    }

    @GetMapping("/delete")
    public WebResult delete(@RequestParam(value = "id") String id,
                            HttpServletRequest request) {
        return fileService.delete(id,request);
    }

    @GetMapping("/get_uploader_by_project")
    public WebResult getUploaderByProject(@RequestParam(value = "projectId") String projectId) {
        return fileService.getUploaderByProject(projectId);
    }

    @GetMapping("/get_state_and_count")
    public WebResult getStateAndCount(@RequestParam(value = "selectProId") String selectProId,
                                      @RequestParam(value = "systemId",required = false) String systemId,
                                      @RequestParam(value = "keyword",required = false) String keyword,
                                      @RequestParam(value = "tagName",required = false) String tagName,
                                      @RequestParam(value = "uploadUserId",required = false) Integer uploadUserId,
                                      HttpServletRequest request) {
        return fileService.getStateAndCount(selectProId,systemId,keyword,tagName,uploadUserId,request);
    }

    @GetMapping("/dtl")
    public WebResult dtl(@RequestParam(value = "id") String id) {
        return fileService.dtl(id);
    }

    @GetMapping("/export")
    public void export(@RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String tagName,
                       @RequestParam(required = false) Integer state,
                       @RequestParam(required = false) String systemId,
                       @RequestParam(required = false) Integer uploadUserId,
                       @RequestParam(required = false) String selectProId,
                       HttpServletRequest request,
                       HttpServletResponse response) throws Exception {
        fileService.export(keyword,tagName,state,systemId,uploadUserId,selectProId,request,response);
    }
}
